package com.ruoyi.web.controller;

import java.util.List;
import java.util.Objects;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.system.service.ISysDeptService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysCourse;
import com.ruoyi.system.service.ISysCourseService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 课程管理Controller
 * 
 * @author ruoyi
 * @date 2025-04-24
 */
@Controller
@RequestMapping("/system/course")
public class SysCourseController extends BaseController
{
    private String prefix = "system/course";

    @Autowired
    private ISysCourseService sysCourseService;

    @Autowired
    private ISysDeptService sysDeptService;

    @RequiresPermissions("system:course:view")
    @GetMapping()
    public String course()
    {
        return prefix + "/course";
    }

    /**
     * 查询课程管理列表
     */
    @RequiresPermissions("system:course:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysCourse sysCourse)
    {
        startPage();
        List<SysCourse> list = sysCourseService.selectSysCourseList(sysCourse);
        return getDataTable(list);
    }

    /**
     * 导出课程管理列表
     */
    @RequiresPermissions("system:course:export")
    @Log(title = "课程管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SysCourse sysCourse)
    {
        List<SysCourse> list = sysCourseService.selectSysCourseList(sysCourse);
        ExcelUtil<SysCourse> util = new ExcelUtil<SysCourse>(SysCourse.class);
        return util.exportExcel(list, "课程管理数据");
    }

    /**
     * 新增课程管理
     */
    @RequiresPermissions("system:course:add")
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        // 查询部门信息 将部门信息设置到上下文中
        List<SysDept> sysDepts = sysDeptService.selectDeptList(new SysDept());
        mmap.put("deptList", sysDepts);
        return prefix + "/add";
    }

    /**
     * 新增保存课程管理
     */
    @RequiresPermissions("system:course:add")
    @Log(title = "课程管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SysCourse sysCourse)
    {
        return toAjax(sysCourseService.insertSysCourse(sysCourse));
    }

    /**
     * 修改课程管理
     */
    @RequiresPermissions("system:course:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        SysCourse sysCourse = sysCourseService.selectSysCourseById(id);
        mmap.put("sysCourse", sysCourse);
        // 查询部门信息 将部门信息设置到上下文中
        List<SysDept> sysDepts = sysDeptService.selectDeptList(new SysDept());
        for (SysDept sysDept : sysDepts) {
            if (Objects.equals(sysCourse.getDeptId(), sysDept.getDeptId())) {
                sysDept.setFlag(true);
            }
        }
        mmap.put("deptList", sysDepts);
        return prefix + "/edit";
    }

    /**
     * 修改保存课程管理
     */
    @RequiresPermissions("system:course:edit")
    @Log(title = "课程管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SysCourse sysCourse)
    {
        return toAjax(sysCourseService.updateSysCourse(sysCourse));
    }

    /**
     * 删除课程管理
     */
    @RequiresPermissions("system:course:remove")
    @Log(title = "课程管理", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(sysCourseService.deleteSysCourseByIds(ids));
    }

    /**
     * 智能生成考题
     * @param id 课程ID
     * @return res
     */
    @ResponseBody
    @PostMapping( "/generation")
    @RequiresPermissions("system:course:test:paper:generation")
    public AjaxResult testPaperGeneration(String id)
    {
        sysCourseService.testPaperGeneration(id);
        return toAjax(1);
    }

    /**
     * 发布课程
     * @param id 课程ID
     * @return res
     */
    @ResponseBody
    @PostMapping( "/releaseCourses")
    @RequiresPermissions("system:course:release:courses")
    public AjaxResult releaseCourses(String id)
    {
        sysCourseService.releaseCourses(id);
        return toAjax(1);
    }

}
